package com.lmh.backend.controller;

import com.lmh.backend.entity.Admin;
import com.lmh.backend.entity.News;
import com.lmh.backend.entity.User;
import com.lmh.backend.service.AdminService;
import com.lmh.backend.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping(value = "/admin")
public class AdminController {
    private AdminService adminService;
    private NewsService newsService;
    @Autowired
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }
    @Autowired
    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String adminRegister(HttpServletRequest request, HttpServletResponse response,
                                RedirectAttributes attributes) {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String password = request.getParameter("password");
        String valid = request.getParameter("valid");
        // 不一致则返回注册界面并提示错误信息
        if(!password.equals(valid)) {
            attributes.addAttribute("passwordError", "两次密码输入不一致");
            return "redirect:/user/register";
        }
        String adminName = request.getParameter("username");
        // 有重复用户名则返回注册界面并提示错误信息
        Admin existAdmin = adminService.getAdminByName(adminName);
        // 无重复用户名则将用户数据存入数据库
        if(existAdmin != null) {
            attributes.addAttribute("usernameError", "用户已存在");
            return "redirect:/user/register";
        }
        Admin newAdmin = new Admin();
        newAdmin.setUsername(adminName);
        newAdmin.setPassword(password);
        adminService.save(newAdmin);
        // 注册成功后将username存入cookie
        Cookie cookie = new Cookie("username", adminName);
        cookie.setMaxAge(60 * 60 * 24);
        cookie.setPath("/");
        response.addCookie(cookie);
        // session存储
        request.getSession().setAttribute("role", "ADMIN");
        return "redirect:home";
    }
    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public String adminLogin(HttpServletRequest request, HttpServletResponse response,
                             RedirectAttributes attributes) {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 查找有无此用户
        String adminName = request.getParameter("username");
        String password = request.getParameter("password");
        Admin realAdmin = adminService.getAdminByName(adminName);
        // 没有则返回登录界面并提示错误信息
        if(realAdmin == null) {
            attributes.addAttribute("notMatchError", "用户名不存在");
            return "redirect:/user/hello";
        }
        // 有则检验密码是否匹配
        if(!realAdmin.getPassword().equals(password)) {
            attributes.addAttribute("notMatchError", "用户名或密码错误");
            return "redirect:/user/hello";
        }
        // 然后将用户名存入cookie
        Cookie cookie = new Cookie("username", adminName);
        cookie.setMaxAge(60 * 60 * 24);
        cookie.setPath("/");
        response.addCookie(cookie);
        // session存储
        request.getSession().setAttribute("role", "ADMIN");
        // 最后重定向到主页
        return "redirect:home";
    }
    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public ModelAndView adminHome(@CookieValue(value = "username", required = false)String adminName) {
        ModelAndView mv = new ModelAndView("adminHome");
        Admin admin = adminService.getAdminByName(adminName);
        mv.addObject("admin", admin);
        return mv;
    }
    @RequestMapping(value = "/checkList", method = RequestMethod.GET)
    public ModelAndView checkList() {
        List<News> uncheckedNews = newsService.getWaitCheckNews();
        ModelAndView mv = new ModelAndView("uncheckedNews");
        mv.addObject("newsList", uncheckedNews);
        return mv;
    }

    @RequestMapping(value = "/exit", method = RequestMethod.GET)
    public String exit(HttpServletResponse response) {
        // 删除cookie
        Cookie cookie = new Cookie("username", null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
        return "redirect:/user/hello";
    }
}
